<?php

namespace App\Http\Controllers\Shop\Product;

use App\Http\Controllers\ShopController;
use App\Models\Business\Business as BusinessModel;
use App\Models\Config as ConfigModel;
use App\Models\Product\Product as ProductModel;
use App\Models\Product\Order\Order as OrderModel;
use App\Models\Product\Comment as CommentModel;
use App\Models\Business\Collection as CollectionModel;
use Illuminate\Support\Facades\Validator;
use Mockery\Undefined;
use Symfony\Component\VarDumper\Cloner\Data;

class ProductController extends ShopController
{
    public function index()
    {
        $page = request('page', 1);
        $limit = request('limit', 10);
        $typeid = request('typeid', 0);
        $keyword = request('keyword', '');
        $flag = request('flag', 0);
        $orderBy = request('orderBy', 'createtime');

        // 封装条件数组
        $where = [
            'status' => '1'
        ];

        if ($typeid) {
            $where['typeid'] = $typeid;
        }

        if ($keyword) {
            // $where['name'] = ['like','%关键字%']
            $where[] = ['name', 'LIKE', "%$keyword%"];
        }

        if ($flag) {
            $where['flag'] = $flag;
        }

        $start = ($page - 1) * $limit;

        $ProductData = ProductModel::where($where)->orderBy($orderBy, 'desc')->offset($start)->limit($limit)->get();

        $ProductCount = ProductModel::where($where)->count();

        $data = [
            'count' => $ProductCount,
            'list' => $ProductData
        ];

        if ($ProductData) {
            return $this->success('查询成功', $data);
        } else {
            return $this->error('暂无商品', []);
        }
    }

    public function info()
    {
        $proid = request('proid', 0);
        $busid = request('busid', 0);

        $Product = ProductModel::where(['status' => '1', 'id' => $proid])->first();
        $comment = CommentModel::with('business')->where(['proid' => $proid])->Where(function($query) {
            $query->where('comment', '<>',null)
                  ->Orwhere('proimg', '<>', null)
                  ->Orwhere('rate', '<>', null);
        })->get();
        // dd($comment);
        if (!$comment) {
            $Product['comment'] = [];
        } else {
            $Product['comment'] = $comment;
        };
        if (!$Product) {
            return $this->error('商品不存在');
        }

        // 判断登录用户是否收藏
        $Product['collection'] = false;

        $business = BusinessModel::find($busid);

        if ($business) {
            $collection = CollectionModel::where(['busid' => $busid, 'proid' => $proid])->first();

            $Product['collection'] = $collection ? true : false;
        }

        return $this->success('查询成功', $Product);
    }



    // public function collection()
    // {
    //     $busid = request('busid',0);
    //     $proid = request('proid',0);
    //     $cateid = request('cateid',null);

    //     $data=[
    //         'busid'=>$busid,
    //         'cateid'=>$cateid,
    //         'proid'=>$proid

    //     ];
    //     // return $data;
    //     $result = CollectionModel::create($data);
    //     if($result === false)
    //     {
    //         return $this->error('收藏失败');
    //     }else{
    //         return $this->success('收藏成功');
    //     }
    // }

    public function collection()
    {
        $proid = request('proid', 0);
        $busid = request('busid', 0);
        // dd($proid);
        $where = [
            'proid' => $proid,
            'busid' => $busid,
        ];

        $collection = CollectionModel::where($where)->first();

        $msg = '未知信息';

        if ($collection) {
            // 已收藏
            $msg = '取消收藏';

            $result = $collection->delete();
        } else {
            // 未收藏
            $data = [
                'busid' => $busid,
                'proid' => $proid
            ];

            $validate = [
                [
                    'busid'  => 'required',
                    'proid'   => 'required'
                ],
                [
                    'busid.required' => '未知用户',
                    'proid.required'     => '未知商品'
                ]
            ];

            $validator = Validator::make($data, ...$validate);

            if ($validator->fails()) {
                return $this->error($validator->errors()->first());
            }

            $msg = '收藏';

            $result = CollectionModel::create($data);
        }

        if ($result === false) {
            return $this->error("{$msg}失败");
        } else {
            return $this->success("{$msg}成功");
        }
    }

    public function comment()
    {

        $params = request()->input();
        // $busid = $params['busid'];
        $rate = $params['rate'];

        $comment = $params['comment'];
        // dd($params['comment']);
        $orderid = $params['orderid'];

        // dd($_FILES['proming']);
        // dd(isset($_FILES['proming']));
        if (isset($_FILES['proming']) && $_FILES['proming']['size'] > 0) {

            $cdn = ConfigModel::where('name', 'url')->value('value');

            $url = $cdn . '/shop/product/product/upload';
            // return $url;
            // 第一个参数属于文件路径 第二个参数是文件类型 第三个是文件名

            $file =  new \CURLFile($_FILES['proming']['tmp_name'], $_FILES['proming']['type'], $_FILES['proming']['name']);
            // dd($file);
            // dd($url);

            $res = httpRequest($url, ['proming' => $file, 'orderid' => $orderid]);

            $proming = json_decode($res, true);
            // dd($proming);
            // $promingData = CommentModel::where('orderid',$orderid)->first();
            // if($promingData['proming'])
            // return $avatar;
            if ($proming['code'] === 0) {
                $result = CommentModel::where(['orderid' => $orderid])->update(['rate' => null, 'comment' => null, 'proimg' => null]);
                $status = OrderModel::where('id', $orderid)->update(['status' => '3']);
                return $this->error($proming['msg']);
            }
            $promingData = CommentModel::where(['orderid' => $orderid])->where('proimg', '<>', null)->first();
            // dd($promingData['proimg']);
            if ($promingData) {
                // dd(111);
                $proming['data'] = $promingData['proimg'] . ',' . $proming['data'];
            }
            // return 1;
            // return $_FILES['avatar'];
            // $data['proming'] = $proming['data'];
            // dd($proming['data']);
        } else {
            $result = CommentModel::where(['orderid' => $orderid])->update(['rate' => $rate, 'comment' => $comment]);
            $status = OrderModel::where('id', $orderid)->update(['status' => '4']);
            if ($result) {

                return $this->success('评价成功');
            } else {
                $result = CommentModel::where(['orderid' => $orderid])->update(['rate' => null, 'comment' => null, 'proimg' => null]);
                $status = OrderModel::where('id', $orderid)->update(['status' => '3']);
                return $this->error('评价失败');
            }
        }

        // dd($proming['data']);
        $result = CommentModel::where(['orderid' => $orderid])->update(['rate' => $rate, 'comment' => $comment, 'proimg' => $proming['data']]);
        $status = OrderModel::where('id', $orderid)->update(['status' => '4']);
        if ($result) {
            return $this->success('评价成功');
        } else {
            $result = CommentModel::where(['orderid' => $orderid])->update(['rate' => null, 'comment' => null, 'proimg' => null]);
            $status = OrderModel::where('id', $orderid)->update(['status' => '3']);
            return $this->error('评价失败');
        }
    }
}
